//
//  SHD_ChipCollectionVeiwCell.m
//  B段项目
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_ChipCollectionVeiwCell.h"

@implementation SHD_ChipCollectionVeiwCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataArray = [NSMutableArray array];
        _chipMnTableView = [[UITableView alloc]init];
    
        _chipMnTableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, 50 *FitHeight)];
        UIImageView *tag = [[UIImageView alloc]initWithFrame:CGRectMake(278 * FitWidth, 0, 32 * FitWidth, 32 *FitHeight)];
        tag.image = [UIImage imageNamed:@"SHD_标签"];
        _chipMnTableView.dataSource= self;
        _chipMnTableView.delegate = self;
        [_chipMnTableView NightWithType:UIViewColorType2];
        [self.contentView addSubview:_chipMnTableView];
//        _chipMnTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            _flag = 0;
//                       
//            [self getData];
//        }];
//        _chipMnTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            _flag += 10;
//            [self getData];
//        }];
        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        _chipMnTableView.header = header;
        NSMutableArray *array = [NSMutableArray array];
        for (NSInteger i = 1; i <= 9; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%lda", i]];
            [array addObject:image];
        }
        [header setImages:array duration:1 forState:MJRefreshStatePulling];
        header.lastUpdatedTimeLabel.hidden = YES;
        [header setTitle:@"加载中 ..." forState:MJRefreshStateRefreshing];
        MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        _chipMnTableView.footer = footer;
        [footer setImages:array duration:1 forState:MJRefreshStateRefreshing];
        [_chipMnTableView.header beginRefreshing];

#pragma mark 标签跳转手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jump)];
        [tag addGestureRecognizer:tap];
        tag.userInteractionEnabled = YES;
        _myView = [[myView alloc]init];
        [_myView NightWithType:UIViewColorType1];
        _myView.alpha = .5;
        [_myView addSubview:tag];
        [self.contentView addSubview:_myView];
        
        _backTopImageView = [[UIImageView alloc]init];
        
        [self.contentView addSubview:_backTopImageView];

        _HUD = [[XMN_ProgressHUD alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Height)];
        _HUD.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.9 alpha:1];
        _HUD.XMN_label.text = @"Waiting...";
        [self.contentView addSubview:_HUD];
    }
    return self;
}
-(void)jump
{
    SHD_TagViewController *tav = [[SHD_TagViewController alloc]init];
    [self.nav pushViewController:tav animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_temp > _satrTemp) {
        [UIView animateWithDuration:0.3 animations:^{
          _myView.frame = CGRectMake(0, - 50 *FitHeight, 375 * FitWidth, 50 * FitHeight);
        }];
    }
    else if (_temp < _satrTemp)
    {
        [UIView animateWithDuration:0.3 animations:^{
            _myView.frame = CGRectMake(0, 0 *FitHeight, 375 * FitWidth, 50 * FitHeight);
        }];

    }
#pragma mark - 回顶部
    
        if (_satrTemp > 10) {
            [UIView animateWithDuration:1 animations:^{
                _backTopImageView.frame = CGRectMake(300 * FitWidth, 520 *FitHeight, 32 * FitWidth, 32 * FitHeight);
            }];
        }
        else
        {
            [UIView animateWithDuration:1 animations:^{
                _backTopImageView.frame = CGRectMake(300 * FitWidth, 700 *FitHeight, 32 * FitWidth, 32 * FitHeight);
            }];
        }

}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _satrTemp = _temp;
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _chipMnTableView.frame = CGRectMake(0, 0 * FitHeight, 375 *FitWidth, (667 - 64) *FitHeight);
    _myView.frame = CGRectMake(0, 0 *FitHeight, 375 * FitWidth, 50 * FitHeight);
    _backTopImageView.frame = CGRectMake(300 * FitWidth, 700 *FitHeight, 32 * FitWidth, 32 * FitHeight);
    _backTopImageView.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    _backTopImageView.image = [UIImage imageNamed:@"SHD_回顶部"];
    _backTopImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backTop)];
    [_backTopImageView addGestureRecognizer:tap];
}
#pragma mark 回顶部触发方法
-(void)backTop
{
    [_chipMnTableView.header beginRefreshing];
}


#pragma mark 网络请求
-(void)getData
{
    if ([_chipMnTableView.header isRefreshing]) {
        _flag = 0;
    }
    else
    {
        _flag += 10;
    }
    
    [HTTPMethod getDataByString:@"http://api2.pianke.me/timeline/list" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&deviceid=5AA12B08-7AD2-4ED0-B4EB-571025443952&limit=10&start=%ld&version=3.0.6", _flag ]] WithDataBlock:^(id data) {
        NSArray *array = [[data objectForKey:@"data"] objectForKey:@"list"];
            if (_flag == 0) {
                
                if (data != nil) {
                    [_dataArray removeAllObjects];
                    for (NSDictionary *dic in array) {
                    
                    SHD_ChipMainModel *chip = [[SHD_ChipMainModel alloc]init];
                    [chip setValuesForKeysWithDictionary:dic];
                    [_dataArray addObject:chip];

                    }
                }
            }
            else
            {
                for (NSDictionary *dic in array) {
                SHD_ChipMainModel *chip = [[SHD_ChipMainModel alloc]init];
                [chip setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:chip];
                }
            }
        [_chipMnTableView.header endRefreshing];
        [_chipMnTableView.footer endRefreshing];
        [_chipMnTableView reloadData];
        [_HUD removeFromSuperview];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _temp = indexPath.row;

    static NSString *identifier = @"cell";
    
    SHD_ChipDataTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell ) {
        cell = [[SHD_ChipDataTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    static NSString *noImageIden = @"noImageCell";
    SHD_NOImageCell *noImageCell = [tableView dequeueReusableCellWithIdentifier:noImageIden];
    if (!noImageCell ) {
        noImageCell = [[SHD_NOImageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:noImageIden];
    }
    
    if (![[_dataArray[indexPath.row] coverimg] isEqualToString:@""])
    {
        
        cell.chipModel = _dataArray[indexPath.row];
        cell.nav = self.nav;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else
    {
        noImageCell.chipModel = _dataArray[indexPath.row];
        noImageCell.selectionStyle = UITableViewCellSelectionStyleNone;
        noImageCell.nav = self.nav;
        return noImageCell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (![[_dataArray[indexPath.row] coverimg] isEqualToString:@""]) {
        NSArray *whArray = [[_dataArray[indexPath.row] coverimg_wh] componentsSeparatedByString:@"*"];
        CGFloat hight = [whArray[1] floatValue] * 345 * FitHeight / [whArray[0] floatValue];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18 * FitWidth], NSFontAttributeName, nil];
        CGRect rect = [[_dataArray[indexPath.row] content] boundingRectWithSize:CGSizeMake(345 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        return hight + ceil(rect.size.height) + 160 * FitHeight;
    }
    else
    {
        
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18], NSFontAttributeName, nil];
        CGRect rect = [[_dataArray[indexPath.row] content] boundingRectWithSize:CGSizeMake(345 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        return  ceil(rect.size.height) + 100 * FitHeight;
        
        
        
    }
}
#pragma mark 点击进入详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMN_PostsViewController *cwv = [[XMN_PostsViewController alloc]init];
    [self.nav pushViewController:cwv animated:YES];
    cwv.idString = [_dataArray[indexPath.row] contentid];
    cwv.contentString = [_dataArray[indexPath.row] content];
    //cwv.titleString = [_dataArray[indexPath.row] title];
}

@end
